package com.xdog.okx.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.alibaba.fastjson.JSONObject;
import com.xdog.common.core.domain.R;
import com.xdog.okx.domain.PositionListQuery;
import com.xdog.okx.domain.TokenListQuery;
import com.xdog.okx.domain.token.TokenTransactionListQuery;
import com.xdog.okx.exception.OkxApiException;
import com.xdog.okx.service.IOkxTokenService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * OKX 代币数据控制器
 */
@Tag(name = "OKX 代币数据", description = "OKX 代币数据控制器")
@RestController
@RequestMapping("/okx/token")
public class OkxTokenController {

    @Resource
    private IOkxTokenService okxTokenService;

    /**
     * 查询代币列表
     */
    @Operation(summary = "查询代币列表", description = "根据链短名称查询代币列表")
    @GetMapping("/list")
    @SaIgnore
    public R<JSONObject> getTokenList(TokenListQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            if (query.getProtocolType() != null) params.put("protocolType", query.getProtocolType());
            if (query.getTokenContractAddress() != null) params.put("tokenContractAddress", query.getTokenContractAddress());
            if (query.getStartTime() != null) params.put("startTime", query.getStartTime());
            if (query.getEndTime() != null) params.put("endTime", query.getEndTime());
            if (query.getOrderBy() != null) params.put("orderBy", query.getOrderBy());
            if (query.getPage() != null) params.put("page", query.getPage());
            if (query.getLimit() != null) params.put("limit", query.getLimit());

            JSONObject tokenList = okxTokenService.getTokenList(params);
            return R.ok(tokenList);
        } catch (OkxApiException e) {
            return R.fail(e.getHttpStatus(), "查询代币列表失败: " + e.getMessage());
        } catch (Exception e) {
            return R.fail("查询代币列表失败: " + e.getMessage());
        }
    }

    /**
     * 查询代币持仓列表
     */
    @Operation(summary = "查询代币持仓列表", description = "根据链短名称和代币合约地址查询代币持仓列表")
    @GetMapping("/position-list")
    public R<JSONObject> getPositionList(PositionListQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            params.put("tokenContractAddress", query.getTokenContractAddress());
            if (query.getHolderAddress() != null) params.put("holderAddress", query.getHolderAddress());
            if (query.getPage() != null) params.put("page", query.getPage());
            if (query.getLimit() != null) params.put("limit", query.getLimit());

            JSONObject positionList = okxTokenService.getPositionList(params);
            return R.ok(positionList);
        } catch (OkxApiException e) {
            return R.fail(e.getHttpStatus(), "查询代币持仓列表失败: " + e.getMessage());
        } catch (Exception e) {
            return R.fail("查询代币持仓列表失败: " + e.getMessage());
        }
    }

    /**
     * 查询代币转账详情
     */
    @Operation(summary = "查询代币转账详情", description = "根据链短名称和代币合约地址查询代币转账详情")
    @GetMapping("/transaction-list")
    public R<JSONObject> getTransactionList(TokenTransactionListQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            params.put("tokenContractAddress", query.getTokenContractAddress());
            if (query.getMaxAmount() != null) params.put("maxAmount", query.getMaxAmount());
            if (query.getMinAmount() != null) params.put("minAmount", query.getMinAmount());
            if (query.getPage() != null) params.put("page", query.getPage());
            if (query.getLimit() != null) params.put("limit", query.getLimit());

            JSONObject transactionList = okxTokenService.getTransactionList(params);
            return R.ok(transactionList);
        } catch (OkxApiException e) {
            return R.fail(e.getHttpStatus(), "查询代币转账详情失败: " + e.getMessage());
        } catch (Exception e) {
            return R.fail("查询代币转账详情失败: " + e.getMessage());
        }
    }
}